<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="auto"
      style="margin: 15px 0px"
    >
      <el-form-item label="模型名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="模型创建背景" prop="background">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.background"-->
      <!--          placeholder="请输入模型创建背景"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="模型业务逻辑" prop="logic">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.logic"-->
      <!--          placeholder="请输入模型业务逻辑"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="主要依据" prop="accord">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.accord"-->
      <!--          placeholder="请输入主要依据"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="数据来源中间表" prop="dataTable">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.dataTable"-->
      <!--          placeholder="请输入数据来源中间表"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="模型应用情况" prop="modelCondition">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.modelCondition"-->
      <!--          placeholder="请输入模型应用情况"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="搭建时间" prop="beginTime">
        <el-date-picker
          clearable
          v-model="queryParams.beginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择搭建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['model:model:add']"
          >新增</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['model:model:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['model:model:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['model:model:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="modelList"
      @selection-change="handleSelectionChange"
      stripe
      style="width: 100%; margin-top: 15px"
    >
      <!--      <el-table-column type="selection"   align="center" />-->
      <!--      <el-table-column label="ID编码" align="center" prop="id" />-->
      <el-table-column
        label="序号"
        type="index"
        :index="indexMethod"
        width="60"
      />
      <el-table-column label="模型名称" align="center" prop="name" />
      <el-table-column label="功能简介" align="center" prop="introduce" />
      <el-table-column label="模型业务逻辑" align="center" prop="logic" />
      <el-table-column
        label="搭建时间"
        align="center"
        prop="beginTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="模型创建背景" align="center" prop="background" />-->
      <!--      <el-table-column label="主要依据" align="center" prop="accord" />-->
      <!--      <el-table-column label="SQL语句" align="center" prop="modelSql" />-->
      <!--      <el-table-column label="数据来源中间表" align="center" prop="dataTable" />-->
      <!--      <el-table-column label="模型应用情况" align="center" prop="modelCondition" />-->
      <el-table-column
        label="部门"
        align="center"
        prop="department"
        width="100"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding 80px"
        width="400"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['model:model:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['model:model:edit']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="success"
            icon="el-icon-mouse"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['model:model:edit']"
            >执行</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:model:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审计模型库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="模型名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentCode">
          <!--          <el-input v-model="form.department" placeholder="请输入部门" />-->
          <treeselect
            v-model="form.departmentCode"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
          />
        </el-form-item>
        <el-form-item label="模型创建背景" prop="background">
          <el-input
            v-model="form.background"
            placeholder="请输入模型创建背景"
          />
        </el-form-item>
        <el-form-item label="模型业务逻辑" prop="logic">
          <el-input v-model="form.logic" placeholder="请输入模型业务逻辑" />
        </el-form-item>
        <el-form-item label="主要依据" prop="accord">
          <el-input v-model="form.accord" placeholder="请输入主要依据" />
        </el-form-item>
        <el-form-item label="SQL语句" prop="modelSql">
          <el-input
            v-model="form.modelSql"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="数据来源中间表" prop="dataTable">
          <el-input
            v-model="form.dataTable"
            placeholder="请输入数据来源中间表"
          />
        </el-form-item>
        <el-form-item label="模型应用情况" prop="modelCondition">
          <el-input
            v-model="form.modelCondition"
            placeholder="请输入模型应用情况"
          />
        </el-form-item>
        <el-form-item label="搭建时间" prop="beginTime">
          <el-date-picker
            clearable
            v-model="form.beginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择搭建时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="功能简介" prop="introduce">
          <el-input
            v-model="form.introduce"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->

    <el-dialog
      :title="title"
      :visible.sync="detail"
      width="1200px"
      append-to-body
    >
      <div style="border: #00afff 4px solid; padding: 20px">
        <div style="font-size: 24px; font-weight: bold; margin-bottom: 15px">
          <span>模型名称：</span>
          <span>{{ this.form.name }}</span>
        </div>
        <div style="margin-bottom: 15px">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 8px">
            搭建背景：
          </div>
          <div style="font-size: 16px; margin-bottom: 8px; color: #000000">
            {{ this.form.background }}
          </div>
        </div>
        <div style="margin-bottom: 15px">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 8px">
            主要依据：
          </div>
          <div style="font-size: 16px; margin-bottom: 8px; color: #000000">
            {{ this.form.accord }}
          </div>
        </div>
        <div style="margin-bottom: 15px">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 8px">
            业务逻辑：
          </div>
          <div style="font-size: 16px; margin-bottom: 8px; color: #000000">
            {{ this.form.logic }}
          </div>
        </div>
        <div style="margin-bottom: 15px">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 8px">
            SQL语句：
          </div>
          <div style="font-size: 16px; margin-bottom: 8px; color: #000000">
            {{ this.form.modelSql }}
          </div>
        </div>
        <div style="margin-bottom: 15px">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 8px">
            数据来源：
          </div>
          <div style="font-size: 16px; margin-bottom: 8px; color: #000000">
            {{ this.form.dataTable }}
          </div>
        </div>
        <div style="margin-bottom: 15px">
          <div style="font-size: 20px; font-weight: bold; margin-bottom: 8px">
            模型应用情况：
          </div>
          <div style="font-size: 16px; margin-bottom: 8px; color: #000000">
            {{ this.form.modelCondition }}
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listModel,
  getModel,
  delModel,
  addModel,
  updateModel,
} from "@/api/model/model";
import { getDept } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deptTreeSelect } from "@/api/system/user";

export default {
  name: "Model",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审计模型库表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        background: null,
        logic: null,
        accord: null,
        modelSql: null,
        dataTable: null,
        modelCondition: null,
        beginTime: null,
        department: null,
        departmentCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "模型名称不能为空", trigger: "blur" },
        ],
      },
      // 是否显示详情弹出层
      detail: false,
      // 部门树选项
      deptOptions: undefined,
    };
  },
  created() {
    const type = this.$route.query.type;
    if (type) {
      this.queryParams.department = type;
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    //序号设置
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * 10 + index + 1;
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
        console.log("this.deptOptions", this.deptOptions);
      });
    },
    /** 查询审计模型库列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then((response) => {
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        background: null,
        logic: null,
        accord: null,
        modelSql: null,
        dataTable: null,
        modelCondition: null,
        beginTime: null,
        createTime: null,
        updateTime: null,
        introduce: null,
        department: null,
        departmentCode: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审计模型库";
    },
    /** 修改按钮操作 */
    handleUpdate(row, num) {
      this.reset();
      const id = row.id || this.ids;
      getModel(id).then((response) => {
        this.form = response.data;
        if (num === 1) {
          this.open = true;
          this.title = "修改审计模型库";
        } else {
          this.detail = true;
          this.title = "审计模型库详情";
        }
      });
    },
    handleDetail(row) {},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateModel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除审计模型库编号为"' + ids + '"的数据项？')
        .then(function () {
          return delModel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "model/model/export",
        {
          ...this.queryParams,
        },
        `model_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
